package com.autumn.scheduler.job.trigger;

import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.JobDetail;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;

import com.autumn.scheduler.domain.entities.AutumnJobDetails;
import com.autumn.scheduler.domain.values.SchedulerValue;
import com.autumn.util.ExceptionUtils;

/**
 * 简单任务触发器
 * 
 * @author 杨昌国 2018-03-20 14:54:38
 */
public class SimpleJobTrigger extends AbstractIntervalJobTrigger {

	/**
	 * 
	 * @param timeInterval
	 * @param unit
	 */
	public SimpleJobTrigger(int timeInterval, IntervalUnit unit) {
		super(timeInterval, unit);
	}

	@Override
	public Trigger createTrigger(JobDetail jobDetail, AutumnJobDetails autumnJobDetail, SchedulerValue schedulerValue) {
		SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule().repeatForever();
		if (this.getUnit().equals(IntervalUnit.MILLISECOND)) {
			scheduleBuilder.withIntervalInMilliseconds(this.getTimeInterval());
		} else if (this.getUnit().equals(IntervalUnit.SECOND)) {
			scheduleBuilder.withIntervalInSeconds(this.getTimeInterval());
		} else if (this.getUnit().equals(IntervalUnit.MINUTE)) {
			scheduleBuilder.withIntervalInMinutes(this.getTimeInterval());
		} else if (this.getUnit().equals(IntervalUnit.HOUR)) {
			scheduleBuilder.withIntervalInHours(this.getTimeInterval());
		} else {
			ExceptionUtils.throwValidationException("时间单位仅支持毫秒、秒、分、小时");
		}		
		autumnJobDetail.setCronExpression("");
		autumnJobDetail.setTimeInterval(this.getTimeInterval());
		autumnJobDetail.setTimeIntervalUnit(this.getUnit().toString());
		return this.createTrigger(jobDetail, autumnJobDetail, schedulerValue, scheduleBuilder);
	}

}
